package com.wyjy.bkd.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


import com.wyjy.bkd.R;
import com.wyjy.bkd.base.BaseActivity;
import com.wyjy.bkd.utils.DensityUtils;

import java.util.ArrayList;


public class SplashActivity extends BaseActivity {


    private ViewPager      mViewPager;
    private LinearLayout   llContainer;
    private ImageView      ivRedPoint;
    private Button         btnStart;
    private RelativeLayout indexRl;

    //图片id集合
    private int[] mImageIds = new int[]{R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};

    private ArrayList<ImageView> mImageViews;

    private int mPointDis;//圆点移动距离

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    }


    @Override
    public void initView() {
        if (mSpUtils.getBoolean("isFirstRun", true)) {
            initViews();
        }
    }


    //初始化布局
    private void initViews() {
        mViewPager = (ViewPager) findViewById(R.id.vp_guide);
        llContainer = (LinearLayout) findViewById(R.id.ll_container);
        ivRedPoint = (ImageView) findViewById(R.id.iv_red_point);
        btnStart = (Button) findViewById(R.id.btn_start);
        indexRl = (RelativeLayout) findViewById(R.id.index_rl);
        indexRl.setVisibility(View.VISIBLE);
        //初始化n张图片的ImageView
        mImageViews = new ArrayList<ImageView>();
        for (int i = 0; i < mImageIds.length; i++) {
            ImageView view = new ImageView(this);
            view.setBackgroundResource(mImageIds[i]);
            mImageViews.add(view);

            //初始化小圆点
            ImageView point = new ImageView(this);
            point.setImageResource(R.drawable.shape_point_normal);

            //初始化布局参数
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            if (i > 0) {
                params.leftMargin = DensityUtils.dip2px(this, 10);
            }

            point.setLayoutParams(params);//设置布局参数

            llContainer.addView(point);
        }

        mViewPager.setAdapter(new GuideAdapter());

        //监听ViewPager滑动事件,更新小红点位置
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                if (position == mImageIds.length - 1) {
                    //最后一个页面显示开始体验按钮
                    btnStart.setVisibility(View.VISIBLE);
                } else {
                    btnStart.setVisibility(View.GONE);
                }
            }

            //监听滑动事件
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

                //通过修改小红点的左边距来更新小红点的位置
                int leftMargin = (int) (mPointDis * positionOffset + position * mPointDis + 0.5f);
                //获取小红点的布局参数
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivRedPoint.getLayoutParams();
                params.leftMargin = leftMargin;
                ivRedPoint.setLayoutParams(params);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        //视图树
        ivRedPoint.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    //一旦视图树的layout方法调用完成, 就会回调此方法
                    @Override
                    public void onGlobalLayout() {
                        //布局位置已经确定,可以拿到位置信息了
                        mPointDis = llContainer.getChildAt(1).getLeft()
                                - llContainer.getChildAt(0).getLeft();
                        System.out.println("mPointDis:" + mPointDis);

                        //移除观察者
                        ivRedPoint.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);
                    }
                });

        //开始体验按钮点击
        btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //在sp中记录访问过引导页的状态
                mSpUtils.putBoolean("isFirstRun", false);
                //跳到主页面
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
                finish();

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        //初始化布局
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view = mImageViews.get(position);

            container.addView(view);

            return view;
        }

        //销毁布局
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }
}
